package com.xhz;

import java.lang.instrument.Instrumentation;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;

import com.xhz.mxbean.Reload;
import com.xhz.reload.Reloader;

public class HotAgent {
    public static void premain(String agentArgs, Instrumentation inst) {
		Trace.info("HotAgent-premain-start");
		
		Trace.info("isReadefineClassesSupported:" +inst.isRedefineClassesSupported());
		regMBean(new Reloader(inst));
	}
	
	public static void agentmain(String agentArgs, Instrumentation inst) {
		premain(agentArgs, inst);
	}
	
	public static void regMBean (Reloader reloader) {
		try {
			javax.management.MBeanServer mbs = java.lang.management.ManagementFactory.getPlatformMBeanServer();
			mbs.registerMBean(new Reload(reloader), new javax.management.ObjectName("HotAgent:type=Reload"));
		} catch (InstanceAlreadyExistsException e) {
			e.printStackTrace();
		} catch (MBeanRegistrationException e) {
			e.printStackTrace();
		} catch (NotCompliantMBeanException e) {
			e.printStackTrace();
		} catch (MalformedObjectNameException e) {
			e.printStackTrace();
		}
	}
}
